
/***
 *tab iframe 高度自适应
 */
var body_height=$(".layui-body").height()-45;
$('.layui-tab-content').height(body_height);
$(window).resize(function() {
    var body_height=$(".layui-body").height()-45;
    $('.layui-tab-content').height(body_height);
    $('iframe').height(body_height);
});

/***
 * 侧边菜单栏收缩展开
 */
$('.tladmin-tree-switch').click(function () {

    var _side=$(document).find('.layui-side');
    if (_side.hasClass('tladmin-sided')) {
        _side.removeClass('tladmin-sided');
        // $('.tladmin-tree-switch a i').html('&#xe68f;');
        $('.layui-body').removeClass('tladmin-body-folded');
        $('.layui-footer').removeClass('tladmin-footer-folded');
    }else {
        _side.addClass('tladmin-sided');
        // $('.tladmin-tree-switch a i').html('&#xe63f;');
        $('.layui-body').addClass('tladmin-body-folded');
        $('.layui-footer').addClass('tladmin-footer-folded');
    }

});





layui.use(['element','layer'], function(){

    var element = layui.element;

    element.on('tabDelete(tab)', function(data){
        console.log(this); //当前Tab标题所在的原始DOM元素
        console.log(data.index); //得到当前Tab的所在下标
        console.log(data.elem); //得到当前的Tab大容器
    });
    element.on('tab(tab)', function(data){//修复切换tab导致iframe滚动条消失的bug
        $('.layui-tab-content .layui-show iframe').height($('.layui-tab-content .layui-show iframe').height() - 1);
        $('.layui-tab-content .layui-show iframe').height($('.layui-tab-content .layui-show iframe').height()+ 1);

    });
    addReload();
});

/***
 * 切换TAB选项卡，如果没有则创建
 * @param obj
 */
function chageTab(obj) {
    var json = $(obj).data('nav');
    var id = $(obj).data('navid');
    var ico = $(obj).children('i').html();
    var element = layui.element;

    if($('[lay-id="'+id+'"]').length>0) {
        element.tabChange('tab',id);
    }else {
        element.tabAdd('tab',{
            title:'<i class="iconfont">'+ico+'</i>'+json.title
            ,content:'<iframe data-iframeid="'+id+'" src="'+json.url+'" width="100%" height="'+body_height+'" frameborder="0"></iframe>'
            ,id:id
        });
        addReload();
        element.tabChange('tab',id);
    }
}

/***
 * 向tab中添加刷新按钮
 */
function addReload() {
    $('.layui-tab').each(function () {
        var othis = $(this)
            ,title=othis.children('.layui-tab-title');
        if (othis.attr('tab-reload')) {
            title.find('li').each(function () {
                var li = $(this);
                if(!li.find('.tladmin-tab-reload')[0]) {
                    var reload=$('<i class="layui-icon layui-unselect tladmin-tab-reload">&#x1002;</i>');
                    reload.on('click',function () {
                        var tab_id=$(this).parent().attr('lay-id');
                        var iframe = $("iframe[data-iframeid='" + tab_id + "']");
                        iframe.attr('src',iframe.attr('src'));
                        layer.msg('刷新成功')
                    });
                    li.children('.layui-tab-close').before(reload);
                }

            });
        }

    });
}


// $('#shouye').html('<iframe data-iframeid="0" src="http://www.layui.com/" width="100%" height="'+body_height+'" frameborder="0"></iframe>');
